import { sysDictionaryEncoApi, energyApi } from '@/api'
import { store } from '@/config'
import { TANPANCHA_DATA_FILL_TYPE, TANPANCHA_DATA_FILL_TYPE_TEXT, TANPANCHA_DATA_SOURCE_TYPE, TANPANCHA_DATA_SOURCE_TYPE_TEXT, TANPANCHA_STATUS, TANPANCHA_STATUS_TEXT, TANPANCHA_TYPE, TANPANCHA_TYPE_TEXT } from '@/constant/modules/tanpancha'

const useTanpanchaStore = defineStore('tanpancha', () => {
  const standards = shallowRef<{ name: string, value: string }[]>([])
  const panchaUnit = ref('tCO2e')

  const emissionTypeOptions = shallowRef([
    { label: TANPANCHA_TYPE_TEXT.EMISSION, value: TANPANCHA_TYPE.EMISSION },
    { label: TANPANCHA_TYPE_TEXT.REDUCTION, value: TANPANCHA_TYPE.REDUCTION },
  ])

  const sourceTypeOptions = shallowRef([
    { label: TANPANCHA_DATA_SOURCE_TYPE_TEXT.AUTOMITIC, value: TANPANCHA_DATA_SOURCE_TYPE.AUTOMITIC },
    { label: TANPANCHA_DATA_SOURCE_TYPE_TEXT.MANUAL, value: TANPANCHA_DATA_SOURCE_TYPE.MANUAL },
  ])

  const fillTypeOptions = shallowRef([
    { label: TANPANCHA_DATA_FILL_TYPE_TEXT.MONTH, value: TANPANCHA_DATA_FILL_TYPE.MONTH },
    { label: TANPANCHA_DATA_FILL_TYPE_TEXT.YEAR, value: TANPANCHA_DATA_FILL_TYPE.YEAR },
  ])

  const emissionSourceOptions = shallowRef<{ label: string, value: any }[]>([])
  const indicatorOptions = shallowRef<{ label: string, value: any, [key: string]: any }[]>([])
  const gasOptions = shallowRef<{
    label: string
    value: any
    [key: string]: any
  }[]>([])

  async function getPanchaUnit() {
    const { data } = await sysDictionaryEncoApi.queryList('TAN_PAI_FANG_DAN_WEI')
    panchaUnit.value = data[0]?.dicValue ?? ''
  }

  async function getStandards() {
    const { data } = await sysDictionaryEncoApi.queryList('protocol')
    standards.value = data.map(item => ({
      name: item.dicValue,
      value: item.dicValue,
    }))
    return data
  }

  async function getEmissionSourceOptions() {
    const { data } = await energyApi.queryList()
    emissionSourceOptions.value = data.map(item => ({
      label: item.name,
      value: item.name,
    }))
    return data
  }
  async function getIndicatorOptions(pointId: number) {
    const { data } = await indicatorApi.loadPointList(pointId)
    const result = data.map(item => ({
      label: item.indicatorName,
      value: item.indicatorNumber,
      unit: item.unit,
    }))
    indicatorOptions.value = result
    return result
  }
  async function getGasOptions() {
    if (gasOptions.value.length > 0)
      return gasOptions.value
    const { data } = await carbonInventoryApi.queryCarbonInventoryGasList()
    gasOptions.value = data.map(v => ({
      label: v.gas,
      value: v.gas,
      ...v,
    }))
    return data
  }

  getPanchaUnit()
  getStandards()

  function getPanchaStatusText(status: TANPANCHA_STATUS) {
    const map = {
      [TANPANCHA_STATUS.ARCHIVED]: TANPANCHA_STATUS_TEXT.ARCHIVED,
      [TANPANCHA_STATUS.REPORTED]: TANPANCHA_STATUS_TEXT.REPORTED,
    }
    return map[status]
  }

  function getEmissionTypeText(type: TANPANCHA_TYPE) {
    const map = {
      [TANPANCHA_TYPE.EMISSION]: TANPANCHA_TYPE_TEXT.EMISSION,
      [TANPANCHA_TYPE.REDUCTION]: TANPANCHA_TYPE_TEXT.REDUCTION,
    }
    return map[type]
  }

  function getSourceTypeText(type: TANPANCHA_DATA_SOURCE_TYPE) {
    const map = {
      [TANPANCHA_DATA_SOURCE_TYPE.AUTOMITIC]: TANPANCHA_DATA_SOURCE_TYPE_TEXT.AUTOMITIC,
      [TANPANCHA_DATA_SOURCE_TYPE.MANUAL]: TANPANCHA_DATA_SOURCE_TYPE_TEXT.MANUAL,
    }
    return map[type]
  }

  function getDataFillTypeText(type: TANPANCHA_DATA_FILL_TYPE) {
    const map = {
      [TANPANCHA_DATA_FILL_TYPE.MONTH]: TANPANCHA_DATA_FILL_TYPE_TEXT.MONTH,
      [TANPANCHA_DATA_FILL_TYPE.YEAR]: TANPANCHA_DATA_FILL_TYPE_TEXT.YEAR,
    }
    return map[type]
  }

  return {
    standards,
    panchaUnit,
    emissionTypeOptions,
    sourceTypeOptions,
    fillTypeOptions,
    emissionSourceOptions,
    indicatorOptions,
    gasOptions,
    getGasOptions,
    getIndicatorOptions,
    getEmissionSourceOptions,
    getPanchaStatusText,
    getSourceTypeText,
    getDataFillTypeText,
    getEmissionTypeText,
  }
})

export function useTanpanchaStoreHook() {
  return useTanpanchaStore(store)
}

export default useTanpanchaStore
